home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-04-22 | 3.7 KB | 138 lines | [TEXT/MACV] |
- GameBoard subclass: #TicTacToeGameBoard
- instanceVariableNames: ''
- classVariableNames: ''
- poolDictionaries: '' !
-
- !TicTacToeGameBoard class methods !
-
- new
- "create a new instance"
- | aBoard |
- aBoard := super new.
- aBoard setWidth:3 height:3.
- aBoard reset.
- ^aBoard.! !
-
-
- !TicTacToeGameBoard methods !
-
- allLegalMoves
- "Answer an OrderedCollection of all legal
- moves. For TicTacToe, any move that is not
- an occupied space is legal"
- | answer |
- answer := OrderedCollection new.
- 1 to: (width*height) do:
- [:i | (positions at: i) isNil
- ifTrue: [answer add: i].
- ].
- ^answer.!
-
- move: m
- "Record a move by player WhoseMove.
- In TicTacToe, any move into a vacant square
- is legal, and three pieces in a row wins."
- | |
- self loggit: ((AllPlayers at: WhoseMove) name) ,
- ' moves ' , (m printPaddedTo: 1).
- (positions at: m) isNil
- ifTrue: [positions at: m put: WhoseMove.
- (self threeAcross: m) |
- (self threeDown: m) |
- (self threeDiagonally: m)
- ifTrue: [^#Win]
- ifFalse: [^#Ok].
- ]
- ifFalse: [^#Error].!
-
- reset
- "reset the board back to the start"
- | |
- positions isNil
- ifTrue:
- [positions := Array new: (width * height)].
- 1 to: (width * height) do:
- [:i | positions at: i put: nil].!
-
- threeAcross: aMove
- "answer whether WhoseMove has three marks
- across, one of which is aMove"
- | rowStart answer |
- rowStart := ((aMove - 1) // 3) * 3 + 1.
- answer := true.
- rowStart to: (rowStart + 2) do: [ :i |
- answer := answer &
- ((positions at: i) = WhoseMove)].
- ^ answer.!
-
- threeDiagonally: aMove
- "answer whether WhoseMove has three marks
- diagonally (aMove is not used)"
- | answer1 answer2 |
- answer1 := true.
- answer2 := true.
- 1 to: 9 by: 4 do: [ :i |
- answer1 := answer1 &
- ((positions at: i) = WhoseMove)].
- 3 to: 7 by: 2 do: [ :i |
- answer2 := answer2 &
- ((positions at: i) = WhoseMove)].
- ^ (answer1 | answer2).!
-
- threeDown: aMove
- "answer whether WhoseMove has three marks down,
- one of which is aMove"
- | colStart answer |
- colStart := (aMove - 1) \\ 3 + 1.
- answer := true.
- colStart to: (colStart + 6) by: 3 do: [ :i |
- answer := answer &
- ((positions at: i) = WhoseMove)].
- ^ answer.! !
-
-
- ComputerPlayer subclass: #TicTacToeComputerPlayer
- instanceVariableNames: ''
- classVariableNames: ''
- poolDictionaries: '' !
-
- !TicTacToeComputerPlayer class methods ! !
-
-
- !TicTacToeComputerPlayer methods ! !
-
-
- HumanPlayer subclass: #TicTacToeHumanPlayer
- instanceVariableNames: ''
- classVariableNames: ''
- poolDictionaries: '' !
-
- !TicTacToeHumanPlayer class methods ! !
-
-
- !TicTacToeHumanPlayer methods !
-
- haveProposedMove: aMove
- "Check for valid format. The format is:
- a single digit giving the space to move
- to."
- | moveResult |
- (aMove = 'win') ifTrue: [self win. ^nil].
- (aMove = 'resign' ) ifTrue: [self resign. ^nil].
- (aMove size) < 1
- ifTrue: [self retryMove]
- ifFalse: [
- (aMove at: 1) isDigit
- ifTrue: [
- moveResult :=
- (TheBoard move: aMove asInteger).
- (moveResult = #Win)
- ifTrue: [self win]
- ifFalse:[ (moveResult = #Ok)
- ifTrue: [ self moveOver ]
- ifFalse:
- [self retryMove].
- ]
- ]
- ifFalse: [self retryMove].
- ]! !